﻿using System;
using System.Text;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData.UI;

namespace Admin.ALarm
{
    public partial class Compute : BaseCommon.BasePage
    {
        protected static object locker = new object();
        protected int year = 0;
        protected int computeType = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            computeType = TaxBiz.Tax.TaxConfigBiz.GetComputeType();
            string action = PageContext.GetRequest("action");
            year = PageContext.GetRequestInt("selYear").HasValue ? PageContext.GetRequestInt("selYear").Value : 0;
            if (!string.IsNullOrEmpty(action))
            {
                lock (locker)
                {
                    ComputeTax(year);
                }
            }
        }

        protected void ComputeTax(int year)
        {
            bool isSuccess = true;
            string message = string.Empty;
            try
            {
                int type = PageContext.GetRequestInt("rdComputeType").HasValue ? PageContext.GetRequestInt("rdComputeType").Value : 0;
                isSuccess = TaxBiz.Tax.TaxConfigBiz.UpdateComputeType(type, UserLogin.UserId);
                if (isSuccess)
                {
                    isSuccess = TaxBiz.Tax.AlarmBiz.ComputeTax(year, ref message);
                }
            }
            catch (Exception e)
            {
                isSuccess = false;
                message = "计算应税过程出错，出错信息" + e.Message;
            }
            if (isSuccess)
            {
                Jscript.AlertAndRedirect(year.ToString() + "年应税计算成功", "Compute.aspx");
            }
            else
            {
                Jscript.AlertAndGoBack(message);
            }
        }

        protected string CreateYear(int year)
        {
            string selectDataTemplate = "<option value='{0}'>{1}</option>";
            int nowYear = DateTime.Now.Year;
            StringBuilder sb = new StringBuilder();
            int selYear = year == 0 ? nowYear : year;
            for (int i = 0; i < 8; i++)
            {
                int optionYear = nowYear - 2 + i;
                if (optionYear == selYear)
                {
                    sb.AppendFormat(selectDataTemplate.Replace("<option", "<option selected='selected'"), optionYear, optionYear.ToString() + "年");
                }
                else
                {
                    sb.AppendFormat(selectDataTemplate, optionYear, optionYear.ToString() + "年");
                }
            }
            return sb.ToString();
        }
    }
}